﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

namespace Miao
{
    /// <summary>
    /// 鼠标3D射线回调类,基于新输入系统
    /// </summary>
    public static class MouseRayManager
    {
        /// <summary>
        /// 用数组是为了分辨左中右鼠标三键
        /// </summary>
        static Dictionary<Collider, Action[]> UserMouseDownDict = new Dictionary<Collider, Action[]>();
        static Dictionary<Collider, Action[]> UserMouseUpDict = new Dictionary<Collider, Action[]>();
        static Dictionary<Collider, Action[]> UserMouseEnterDict = new Dictionary<Collider, Action[]>();

        static RaycastHit[] hitArray = new RaycastHit[10];
        static MouseRayManager()
        {
            MiaoRuntime.Updater.AddUpdate(Update);
        }

        static public void AddMouseDown(in Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseDownDict.AddAction(collider, action, mouseButton);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="collider"></param>
        /// <param name="action"></param>
        /// <param name="mouseButton3键为进入既触发"></param>
        static public void AddMouseEnter(in Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseEnterDict.AddAction(collider, action, mouseButton,4);
        }
        static public void AddMouseUp(in Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseUpDict.AddAction(collider, action, mouseButton);
        }
        static public void RemoveMouseDown(Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseDownDict.RemoveAction(collider, action, mouseButton);
        }
        static public void RemoveMouseEnter(Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseEnterDict.RemoveAction(collider, action, mouseButton);
        }
        static public void RemoveMouseUp(Collider collider, Action action, int mouseButton = 0)
        {
            UserMouseUpDict.RemoveAction(collider, action, mouseButton);
        }
        static public void RemoveMouseDownAll(Collider collider)
        {
            UserMouseDownDict.Remove(collider);

        }
        static public void RemoveMouseEnterAll(Collider collider)
        {
            UserMouseEnterDict.Remove(collider);
        }
        static public void RemoveMouseUpAll(Collider collider)
        {
            UserMouseUpDict.Remove(collider);
        }

        public static void Update()
        {
            Ray point;
#if ENABLE_INPUT_SYSTEM
            point = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
#else
   point = Camera.main.ScreenPointToRay(Input.mousePosition);
#endif

            int length = Physics.RaycastNonAlloc(point, hitArray);
            int index = -1;

#if ENABLE_INPUT_SYSTEM

            #region 新输入系统
            #region 鼠标按下

            if (Mouse.current.leftButton.wasPressedThisFrame) index = 0;
            else if (Mouse.current.middleButton.wasPressedThisFrame) index = 1;
            else if (Mouse.current.rightButton.wasPressedThisFrame) index = 2;
            if (index != -1)
            {
                for (int i = 0; i < length; i++)
                {
                    if (UserMouseDownDict.ContainsKey(hitArray[i].collider))
                    {
                        UserMouseDownDict[hitArray[i].collider][index].Invoke();
                    }
                }
                return;
            }
            #endregion
            #region 鼠标释放
            if (Mouse.current.leftButton.wasReleasedThisFrame) index = 0;
            else if (Mouse.current.middleButton.wasReleasedThisFrame) index = 1;
            else if (Mouse.current.rightButton.wasReleasedThisFrame) index = 2;

            else if (index != -1)
            {
                for (int i = 0; i < length; i++)
                {
                    if (UserMouseUpDict.ContainsKey(hitArray[i].collider))
                    {
                        UserMouseUpDict[hitArray[i].collider][index].Invoke();
                    }
                }
                return;
            }
            #endregion
            #region 鼠标维持
            if (Mouse.current.leftButton.isPressed) index = 0;
            else if (Mouse.current.middleButton.isPressed) index = 1;
            else if (Mouse.current.rightButton.isPressed) index = 2;
            else index = 3;
            if (index != -1)
            {
                for (int i = 0; i < length; i++)
                {
                    if (UserMouseEnterDict.ContainsKey(hitArray[i].collider))
                    {
                        UserMouseEnterDict[hitArray[i].collider][index].Invoke();
                    }
                }
                return;
            }
            #endregion
            #endregion
#else
            #region 旧输入系统
            //#region 鼠标按下
            //if ((Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2)))
            //{
            //    if (Input.GetMouseButtonDown(0)) index = 0;
            //    else if (Input.GetMouseButtonDown(1)) index = 1;
            //    else if (Input.GetMouseButtonDown(2)) index = 2;

            //    for (int i = 0; i < hitArray.Length; i++)
            //    {
            //        if (UserMouseDownDict.ContainsKey(hitArray[i].collider))
            //        {
            //            UserMouseDownDict[hitArray[i].collider][index].Invoke();
            //        }
            //    }
            //}
            //#endregion
            //#region 鼠标维持
            //else if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2))
            //{
            //    if (Input.GetMouseButton(0)) index = 0;
            //    else if (Input.GetMouseButton(1)) index = 1;
            //    else if (Input.GetMouseButton(2)) index = 2;

            //    for (int i = 0; i < length; i++)
            //    {
            //        if (UserMouseEnterDict.ContainsKey(hitArray[i].collider))
            //        {
            //            UserMouseEnterDict[hitArray[i].collider][index].Invoke();
            //        }
            //    }
            //}
            //#endregion
            //#region 鼠标释放
            //else if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))
            //{
            //    if (Input.GetMouseButtonUp(0)) index = 0;
            //    else if (Input.GetMouseButtonUp(1)) index = 1;
            //    else if (Input.GetMouseButtonUp(2)) index = 2;
            //    for (int i = 0; i < length; i++)
            //    {
            //        if (UserMouseUpDict.ContainsKey(hitArray[i].collider))
            //        {
            //            UserMouseUpDict[hitArray[i].collider][index].Invoke();
            //        }
            //    }
            //}
            //#endregion
            #endregion
#endif

        }

    }

}
